package com.loan.common;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroInterceptor;
import com.jfinal.ext.plugin.shiro.ShiroPlugin;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.tx.TxByMethodRegex;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.template.Engine;
import com.loan.controller.AppController;
import com.loan.controller.BankController;
import com.loan.controller.CompanyController;
import com.loan.controller.FileController;
import com.loan.controller.InvestLogController;
import com.loan.controller.LoanController;
import com.loan.controller.LoggerController;
import com.loan.controller.NoticeController;
import com.loan.controller.ParamController;
import com.loan.controller.RepayController;
import com.loan.controller.RoleController;
import com.loan.controller.SliderController;
import com.loan.controller.SmsController;
import com.loan.controller.StaffController;
import com.loan.controller.SystemController;
import com.loan.controller.UserController;
import com.loan.inceptor.TokenInceptor;
import com.loan.model._MappingKit;
import com.loan.task.RepayExpireTask;

/**
 * API引导式配置
 */
public class CoreConfig extends JFinalConfig {
    /**
     * 供Shiro插件使用。
     */
    Routes routes;
     
	/** 
	 * 配置常量   
	 */ 
	@Override
	public void configConstant(Constants me) {
		// 加载少量必要配置，随后可用PropKit.get(...)获取值
		PropKit.use("app-config.txt"); 
		me.setBaseUploadPath("attachment/upload/");
		me.setDevMode(PropKit.getBoolean("devMode", true));
		me.setMaxPostSize(99999999);
	}  
	
	/** 
	 * 配置路由 
	 */ 
	@Override
	public void configRoute(Routes me) {
		this.routes = me; 
		me.add("/", UserController.class,"/"); 
		me.add("/app", AppController.class); 
		me.add("/file", FileController.class); 
		me.add("/user", UserController.class); 
		me.add("/loan", LoanController.class); 
		me.add("/repay", RepayController.class);  
		me.add("/sms", SmsController.class);  
		me.add("/bank", BankController.class); 
		me.add("/logger", LoggerController.class); 
		me.add("/slider", SliderController.class); 
		me.add("/investLog", InvestLogController.class); 
		me.add("/company", CompanyController.class);  
		me.add("/staff", StaffController.class);  
		me.add("/sys", SystemController.class);  
		me.add("/notice", NoticeController.class);  
		me.add("/param", ParamController.class);  
		me.add("/role", RoleController.class);   
	}
	
	@Override 
	public void configEngine(Engine me) {
		me.addSharedObject("path", "/loan/");
		me.addSharedObject("assets", "/loan/view/static");
	} 
	
	public static DruidPlugin createDruidPlugin() {
		return new DruidPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
	}
	
	/**  
	 * 配置插件 
	 */
	@Override
	public void configPlugin(Plugins me) {
		
		// 配置C3p0数据库连接池插件
		DruidPlugin druidPlugin = createDruidPlugin();
		me.add(druidPlugin);
		
		// 配置ActiveRecord插件
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		// 所有映射在 MappingKit 中自动化搞定
		_MappingKit.mapping(arp);
		me.add(arp);
		
		//RedisPlugin newsRedis = new RedisPlugin("news", "localhost");
		//me.add(newsRedis);

		Cron4jPlugin task = new Cron4jPlugin(); 
		task.addTask("0 */1 * * *", new RepayExpireTask()); 
		me.add(task); 
		
		// 加载Shiro插件
		// me.add(new ShiroPlugin(routes));
		ShiroPlugin shiroPlugin = new ShiroPlugin(this.routes);
		shiroPlugin.setLoginUrl("/sys/toLoign");
		shiroPlugin.setSuccessUrl("/index.do");
		shiroPlugin.setUnauthorizedUrl("/sys/unAuth");
		me.add(shiroPlugin);
	}
	
	/**
	 * 配置全局拦截器
	 */
	@Override
	public void configInterceptor(Interceptors me) {
		me.addGlobalActionInterceptor(new TokenInceptor());
		me.add(new SessionInViewInterceptor());  
		me.add(new TxByMethodRegex("(.*save.*|.*update.*)"));
		me.add(new ShiroInterceptor());
	} 
	 
	/**
	 * 配置处理器
	 */
	@Override
	public void configHandler(Handlers me) {
		
	}
}
